import { apiClient } from '../config'
import type {
  Response,
  GetDepartmentPagedListAsyncParams,
  GetDepartmentPagedListAsyncResponse,
  AddDepartmentAsyncParams,
  UpdateDepartmentAsyncParams,
} from '../../types/Department'

/**
 * 分页查询部门列表
 * @param params 查询参数
 * @returns 部门列表响应
 */
export const getDepartmentPagedListAsync = async (
  params: GetDepartmentPagedListAsyncParams & { pageIndex: number; pageSize: number },
): Promise<GetDepartmentPagedListAsyncResponse> => {
  return await apiClient.get('/Department/GetDepartmentPagedListAsync', { params })
}

/**
 * 添加部门
 * @param params 添加部门参数
 * @returns 操作结果
 */
export const addDepartmentAsync = async (params: AddDepartmentAsyncParams): Promise<Response> => {
  return await apiClient.post('/Department/AddDepartmentAsync', params, {
    headers: {
      'Content-Type': 'application/json',
    },
  })
}

/**
 * 修改部门信息
 * @param params 修改部门参数
 * @returns 操作结果
 */
export const updateDepartmentAsync = async (
  params: UpdateDepartmentAsyncParams,
): Promise<Response> => {
  return await apiClient.post('/Department/UpdateDepartmentAsync', params, {
    headers: {
      'Content-Type': 'application/json',
    },
  })
}

/**
 * 启用/停用部门
 * @param id 部门ID
 * @returns 操作结果
 */
export const updateDepartmentStatusAsync = async (id: number): Promise<Response> => {
  return await apiClient.post('/Department/UpdateDepartmentStatusAsync', id, {
    headers: {
      'Content-Type': 'application/json',
    },
  })
}

/**
 * 批量启用/停用部门
 * @param ids 部门ID数组
 * @returns 操作结果
 */
export const updateDepartmentsStatusAsync = async (ids: number[]): Promise<Response> => {
  return await apiClient.post('/Department/UpdateDepartmentsStatusAsync', ids, {
    headers: {
      'Content-Type': 'application/json',
    },
  })
}

/**
 * 部门下拉框
 * @param departmentId  部门ID（可选）
 * @returns 操作结果
 */
export const getDepartmentDropdownBoxAsync = async (departmentId?: number): Promise<Response> => {
  return await apiClient.get('/Department/getDepartmentDropdownBoxAsync', {
    params: departmentId ? { departmentId } : {},
  })
}

export default {
  getDepartmentPagedListAsync,
  addDepartmentAsync,
  updateDepartmentAsync,
  updateDepartmentStatusAsync,
  updateDepartmentsStatusAsync,
  getDepartmentDropdownBoxAsync,
}
